$(function(){
	var department_array =new Array();

	var role_array=new Array();
	//初始化
	Initialization();

    //添加用户
	admin_user_modal=function (){
		//显示遮罩层
		$("#modal-demo").modal("show");
	}

	//初始化
	function Initialization(){
		GetInitialData()
		SetInputValue();
	}
	//获取初始数据
	function GetInitialData(){
		$.post("./php/admin-role.php",{action:4},function(json){
			obj=JSON.parse(json);
			department_array=obj.department;
			role_array=obj.role;
		});
	}

	//获取url参数
	function GetQueryString(name){
		var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if(r!=null)return unescape(r[2]); return null;
	}

	//设置输入框的值
	function SetInputValue(){
		var parameter=new Object();
		var role=GetQueryString("role");
		var search=GetQueryString("search");
		if(role !=null && role.toString().length>=1){
			$("#role_select").val(role);
		}
		if(search !=null && search.toString().length>=1){
			$("#search").val(search);
		}
	}

	//跳页
	$("select").change(function(){
		var url='./admin-role.php?role=';
		var id=$(this).attr("id");
		switch(id){
			case 'dormitory_page':
				url=url+$("#role_select").val()+'&page='+$(this).val()+'&search='+$("#search").val();
				window.location.href=url;
			break;
			default:break;
		}
	});

	//查找
	/*$("#btn-search").click(function(){
		var url='./admin-role.php?role=';
		var role=$("#role_select").val();
		var search=$("#search").val();

		if(search == ''){
			window.location.href=url+role+'&page=0&pageSize=20';
		}else{
			window.location.href=url+role+'&page=0&pageSize=20&search='+$("#search").val();
		}
		
	});*/

	//添加用户
	$("#add-user").click(function(){
		var input_null=false;
		var role=$("#modal_select_role").val();
		var uid=$("#modal_uid").val();
		var password=$("#modal_password").val();
		var name=$("#modal_name").val();
		var phone=$("#modal_phone").val();
		if(uid==''){
			$("#modal_uid").addClass('error');
			$("#modal_uid-error").show();
			input_null=true;
		}
		if(password==''){
			$("#modal_password").addClass('error');
			$("#modal_password-error").show();
			input_null=true;
		}
		if(name==''){
			$("#modal_name").addClass('error');
			$("#modal_name-error").show();
			input_null=true;
		}
		if(phone==''){
			$("#modal_phone").addClass('error');
			$("#modal_phone-error").show();
			input_null=true;
		}

		//获取选中的部门
		var count=0;
		var department=new Array();
		$("input[name='department']:checkbox").each(function () {
			//alert($(this).prop("checked"));
			if ($(this).prop("checked")) {
				department[count] = $(this).val();
				count++;
			}
		});
		if(department.length==0){
			layer.msg('未选择任何部门', {
				time: 3000, //3s后自动关闭
				btn: ['确定','取消']
			},function(){
			},function(){
			});
			input_null=true;
		}
		if(input_null){
			return;
		}

		userInformation=new Object();
		userInformation.department=new Array();
		userInformation.role=role;
		userInformation.uid=uid;
		userInformation.password=password;//CryptoJS.MD5(password).toString(CryptoJS.enc.Hex);
		userInformation.name=name;
		userInformation.phone=phone;
		userInformation.department=department;
		userJsonString=JSON.stringify(userInformation);

		$.post("./php/admin-role.php",{action:1,data:userJsonString},function(result){
			//alert(result);
			resultObject=JSON.parse(result);
			switch(resultObject.code){
				case '2001':
					layer.msg(resultObject.message, {
						time: 3000, //3s后自动关闭
						btn: ['确定','取消']
					},function(){
						//$("#modal-demo").modal("toggle");
						location.replace(location.href);
					},function(){
						//$("#modal-demo").modal("toggle");
						location.replace(location.href);
					});
				  	break;
				case '2002':
					$("#modal_uid").addClass('error');
					$("#modal_uid-error").show();
					$("#modal_uid-error").html(resultObject.message);
					break;
				//数据验证不通过
				case '2003':
					input_name=[
						{name:"uid",id:"#modal_uid",error_id:"#modal_uid-error"},
						{name:"password",id:"#modal_password",error_id:"#modal_password-error"},
						{name:"name",id:"#modal_name",error_id:"#modal_name-error"},
						{name:"phone",id:"#modal_phone",error_id:"#modal_phone-error"}
					];
					//alert(resultObject.error);
					error=JSON.parse(resultObject.error);
					for(i=0;i<error.length;i++){
						for(j=0;j<input_name.length;j++){
							object=input_name[j];
							if(error[i]==object.name){
								$(object.id).addClass('error');
								$(object.error_id).show();
								$(object.error_id).html("输入不符合要求");
								break;
							}
						}
					}
					break;
				//添加失败
				case '2004':
					layer.msg(resultObject.message, {
						time: 3000, //3s后自动关闭
						btn: ['确定','取消']
					},function(){
						$("#modal-demo").modal("toggle")
					},function(){
						$("#modal-demo").modal("toggle")
					});
					break;
			}
		});
	});

	//恢复默认样式
	$("input").click(function(){
		$(this).removeClass('error');
		var val=$(this).attr("id");
		var label_id="#"+val+"-error";
		$(label_id).hide();
	});

	//编辑角色,设置弹出层
	admin_role_edit=function(tr,uid){
		//alert($(tr).parents("tr").find(".uid").html());
		$("#update_uid").val($(tr).parents("tr").find(".uid").html());
		$("#update_name").val($(tr).parents("tr").find(".name").html());
		$("#update_phone").val($(tr).parents("tr").find(".phone").html());
		role=$(tr).parents("tr").find(".role").html();
		for(var i=0;i<role_array.length;i++){
			if(role==role_array[i]['name']){
				$("#update_select_role").val(role_array[i]['value']);
			}
		}
		department=($(tr).parents("tr").find(".department").html()).split("、");
		for(var i=0;i<department.length;i++){
			for(var j=0;j<department_array.length;j++){
				if(department[i]==department_array[j]['name']){
					//alert(department_array[j]['name']);
					$(':checkbox[name=update_department]').each(function(index,e){  
						if($(this).val()==department_array[j]['value']){
							$(this).prop("checked",true);
						}else{
							$(this).prop("checked",false);
						}
					});
				}
			}
		}
		
		$("#modal-update").modal("show");
	}

	//更新用户信息
	function updateUserInformation(){
		//alert();
		input_null=false;
		userObject=new Object();
		userObject.role=$("#update_select_role").val();
		userObject.uid=$("#update_uid").val();
		userObject.name=$("#update_name").val();
		userObject.phone=$("#update_phone").val();
		userObject.departments=new Array();
		$(':checkbox[name=update_department]:checked').each(function(index,e){  
			userObject.departments[index]=$(this).val();
		});

		if(userObject.uid==''){
			$("#update_uid").addClass('error');
			$("#update_uid-error").show();
			input_null=true;
		}
		/*if(userObject.password==''){
			$("#modal_password").addClass('error');
			$("#modal_password-error").show();
			input_null=true;
		}*/
		if(userObject.name==''){
			$("#update_name").addClass('error');
			$("#update_name-error").show();
			input_null=true;
		}
		if(!(/^1[34578]\d{9}$/.test(userObject.phone))){
			$("#update_phone").addClass('error');
			$("#update_phone-error").show();
			input_null=true;
		}
		if(userObject.departments.length==0){
			layer.msg('未选择任何部门', {
				time: 3000, //3s后自动关闭
				btn: ['确定','取消']
			},function(){
			},function(){
			});
			input_null=true;
		}
		if(!input_null){
			userJson=JSON.stringify(userObject);
			//更新信息
			$.post("./php/admin-role.php",{action:2,data:userJson},function(jsonString){
				jsonObject=JSON.parse(jsonString);
				switch(jsonObject.code){
					case '2001':
						layer.msg(jsonObject.message, {
							time: 3000, //3s后自动关闭
							btn: ['确定','取消']
						},function(){
							//$("#modal-update").modal("toggle");
							location.replace(location.href);
						},function(){
							//$("#modal-update").modal("toggle");
							location.replace(location.href);
						});
						break;
					case '2002':
						$("#modal_uid").addClass('error');
						$("#modal_uid-error").show();
						$("#modal_uid-error").html(jsonObject.message);
						break;
					//数据验证不通过
					case '2003':
						input_name=[
							{name:"uid",id:"#update_uid",error_id:"#update_uid-error"},
							{name:"password",id:"#update_password",error_id:"#update_password-error"},
							{name:"name",id:"#update_name",error_id:"#update_name-error"},
							{name:"phone",id:"#update_phone",error_id:"#update_phone-error"}
						];
						//alert(jsonObject.error);
						error=JSON.parse(jsonObject.error);
						for(i=0;i<error.length;i++){
							for(j=0;j<input_name.length;j++){
								object=input_name[j];
								if(error[i]==object.name){
									$(object.id).addClass('error');
									$(object.error_id).show();
									$(object.error_id).html("输入不符合要求");
									break;
								}
							}
						}
						break;
					//添加失败
					case '2004':
						layer.msg(jsonObject.message, {
							time: 3000, //3s后自动关闭
							btn: ['确定','取消']
						},function(){
							$("#modal-update").modal("toggle")
						},function(){
							$("#modal-update").modal("toggle")
						});
						break;
				}
			});
		}
		
	}

	$("#update-user").click(function(){
		updateUserInformation();
	});

	//删除角色
	admin_role_del=function(obj,id){
		dataObject=new Object();
		dataObject.users=new Array();
		dataObject.users[0]=id;
		dataJson=JSON.stringify(dataObject);
		layer.confirm('确定删除该用户？',function(index){
			$.post("./php/admin-role.php",{action:3,data:dataJson},function(result){
				resultObject=JSON.parse(result);
				switch(resultObject.code){
					case '2001':
						$(obj).parents("tr").remove();
						layer.msg('已删除!',{icon:1,time:1000});
					break;
					case '2004':
						layer.msg('删除失败!',{icon:1,time:2000});
					break;
				}
			});
		});
	}

	//批量删除
	admin_role_delete=function(){
		dataObject=new Object();
		dataObject.users=new Array();
		dataObject.users=getSelectedUsers();
		if(dataObject.users.length>0){
			dataJson=JSON.stringify(dataObject);
			//弹出确认框
			layer.confirm('确定删除选中用户？',function(index){
				$.post("./php/admin-role.php",{action:3,data:dataJson},function(result){
					resultObject=JSON.parse(result);
					switch(resultObject.code){
						case '2001':
							layer.msg('已删除!',{icon:1,time:1000});
							deleteSelectedTableLine()
						break;
						case '2004':
							layer.msg('删除失败!',{icon:1,time:2000});
						break;
					}
				});
			});
		}else{
			$.Huimodalalert('未选中任何项',2000);
		}
		
	}

	//获取选中用户
	function getSelectedUsers(){
		users=new Array();
		$(':checkbox[name=users_checkbox]:checked').each(function(index,e){  
			users[index]=$(this).val();
		});

		return users;
	}

	//删除选中的表格的行
	function deleteSelectedTableLine(){
		$(':checkbox[name=users_checkbox]:checked').each(function(index,e){  
			$(this).parents("tr").remove();
		});
	}
});